﻿package app.view.components.sections.expo 
{
	import app.model.RemoteProxy;
	import app.model.types.SectionType;
	import app.model.vo.SectionVO;
	import app.view.components.buttons.GlamurBtnBase;
	import app.view.components.guiDima.SponsorsItem;
	import app.view.components.header.HeadMarkerBase;
	import app.view.components.sections.LabeledSectionBase;
	import app.view.events.UIEvent;
	import com.snzflash.components.grid.Grid;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	/**
	 * 1.1.3.1 ОРГАНИЗАТОРЫ
	 * @author Dmitryi_Koruagin, icq:585466579, dmitryikoruagin@mail.ru, skype: ok_ru1
	 */
	public class ExpoOrganizersDBase extends LabeledSectionBase
	{
		// app.view.components.sections.expo.ExpoOrganizersDBase
		
		public var sponsori_Btn:GlamurBtnBase;
		public var partneri_Btn:GlamurBtnBase;
		
		private var _grid:Grid;
		private var _dataItems:Array;
		
		public function ExpoOrganizersDBase() 
		{
			super();
			
		}
		
		override protected function init():void
		{
			super.init();
			
			var colr:Number = 6;
			var colc:Number = 3;
			var gh:Number = 20;
			var gv:Number = 20;
			var w:Number = SponsorsItem.WIDTH*colr + (colr - 1) * gh+5;
			var h:Number = SponsorsItem.HEIGHT*colc + (colc - 1) * gv+5;
			
			//trace('w=', w);
			// настроим таблицу
			_grid = new Grid();
			_grid.width = w;
			_grid.height = h;
			_grid.numRows = colc;
			_grid.numColumns = colr;
			_grid.horizontalGap = gh;
			_grid.verticalGap = gv;
			_grid.itemRenderer = SponsorsItem;
			addChild(_grid);
			_grid.x = 175;
			_grid.y = 310;
			
		}
		
		private function setEvents():void
		{
			sponsori_Btn.addEventListener(MouseEvent.CLICK, onCLickSponsoriBtn);
			partneri_Btn.addEventListener(MouseEvent.CLICK, onCLickPartneriBtn);
			
			addEventListener(UIEvent.CLICK_ITEM_EVENT, onClickGridItem, true);
		}
		
		private function removeEvents():void
		{
			sponsori_Btn.removeEventListener(MouseEvent.CLICK, onCLickSponsoriBtn);
			partneri_Btn.removeEventListener(MouseEvent.CLICK, onCLickPartneriBtn);
			
			removeEventListener(UIEvent.CLICK_ITEM_EVENT, onClickGridItem, true);
		}
		
		override public function onDestroy():void 
		{
			// чистим что тут есть и вызываем супера
			removeEvents();
			
			sponsori_Btn.destroy();
			partneri_Btn.destroy();
			
			_grid.destroy();
			
			
			super.onDestroy();
		};
		
		override public function get data():Object { return _data; };
		
		override public function set data(value:Object):void 
		{
			super.data = value;
			
			
			setParamHead(HeadMarkerBase.BGD_RED);
			
			sponsori_Btn.setLang(_data.lang);
			partneri_Btn.setLang(_data.lang);
			
			
			_dataItems = new Array();
			
			var ind:int = 0;
			for each(var object:* in _data.data.image_text)
			{
				var obj:Object = new Object();
				obj.id = ind;
				obj.name = object.name;
				obj.text = String(object.text).substr(1);
				object.image[1].image_path!='' ? obj.image=RemoteProxy.DOMEN+object.image[1].image_path : obj.image=''
				
				//trace('obj',object.name);
				//trace('obj',object.image[1].image_path);
				//trace('obj', object.text);
				
				//trace('----------------------');
				_dataItems.push(obj);
				ind++;
			}
			
			_grid.dataProvider = _dataItems;
			
			setEvents();
		};
		
		
		/**
		 * переходим в спонсороы
		 * @param	e
		 */
		private function onCLickSponsoriBtn(e:MouseEvent):void 
		{
			removeEvents();
			
			var obj:SectionVO = new SectionVO();
			obj.id = SectionType.PAGE_EXPO_SPONSORS;
			obj.currentId = id;
			obj.params = null;
			
			var ev:UIEvent = new UIEvent(UIEvent.SECTION_CHANGE);
			ev.data = obj;
			dispatchEvent(ev);
		}
		/**
		 * переходим в партнеры
		 * @param	e
		 */
		private function onCLickPartneriBtn(e:MouseEvent):void 
		{
			removeEvents();
			
			var obj:SectionVO = new SectionVO();
			obj.id = SectionType.PAGE_EXPO_PARTNERS;
			obj.currentId = id;
			obj.params = null;
			
			var ev:UIEvent = new UIEvent(UIEvent.SECTION_CHANGE);
			ev.data = obj;
			dispatchEvent(ev);
		}
		
		/**
		 * клик по ячейки таблицы
		 * @param	e
		 */
		private function onClickGridItem(e:UIEvent):void 
		{
			//trace('555', e.data.id);
			
			removeEvents();
			
			var objInfo:Object = new Object();
			
			//trace('777', _data.params.selected.fullDateString);
			
			objInfo.data = _data;
			objInfo.selectedId = e.data.id;
			
			var obj:SectionVO = new SectionVO();
			obj.id = SectionType.PAGE_EXPO_ONE_ORGANIZER;
			obj.currentId = id;
			obj.params = objInfo;
			
			var ev:UIEvent = new UIEvent(UIEvent.SECTION_CHANGE);
			ev.data = obj;
			dispatchEvent(ev);
			
			
		}
	}

}